package com.longcai.piaojia.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.TextView;

import com.longcai.piaojia.R;
import com.longcai.piaojia.fragment.FragmentBusinessOrder;
import com.longcai.piaojia.fragment.FragmentTicketOrder;
import com.zcx.helper.bound.BoundView;

/**
 * Created by Administrator on 2016/8/2.
 */
public class MyOrderListActivity extends BaseActivity implements View.OnClickListener {

    @BoundView(R.id.myorder_ticket_order_tv) private TextView myorder_ticket_order_tv;
    @BoundView(R.id.myorder_business_order_tv) private TextView myorder_business_order_tv;

    private FragmentTicketOrder fragmentTicketOrder;
    private FragmentBusinessOrder fragmentBusinessOrder;

    private Fragment[] fragments;
    private int prePosition = 0;
    private int index;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_myorder);

        setBackTrue();

        setTitleName(getString(R.string.myorder_title_text));

        myorder_ticket_order_tv.setOnClickListener(this);
        myorder_business_order_tv.setOnClickListener(this);

        fragmentTicketOrder = new FragmentTicketOrder();
        fragmentBusinessOrder = new FragmentBusinessOrder();

        fragments = new Fragment[]{fragmentTicketOrder, fragmentBusinessOrder};

        getSupportFragmentManager().beginTransaction().add(R.id.myorder_container_rl, fragmentTicketOrder).show(fragmentTicketOrder).commit();
        myorder_ticket_order_tv.setTextColor(getResources().getColor(R.color.theme_color));


    }

    @Override
    public void onClick(View view) {

        FragmentTransaction trx = getSupportFragmentManager().beginTransaction();

        switch (view.getId()) {
            case R.id.myorder_ticket_order_tv:

                index = 0;

                if (prePosition != index) {

                    myorder_ticket_order_tv.setTextColor(getResources().getColor(R.color.theme_color));
                    myorder_business_order_tv.setTextColor(getResources().getColor(R.color.text_color));

                    trx.hide(fragments[prePosition]);
                    if (!fragments[index].isAdded()) {
                        trx.add(R.id.myorder_container_rl, fragments[index]);
                    }
                    trx.show(fragments[index]).commit();

                    prePosition = index;
                }

                break;

            case R.id.myorder_business_order_tv:

                index = 1;

                if (prePosition != index) {

                    myorder_ticket_order_tv.setTextColor(getResources().getColor(R.color.text_color));
                    myorder_business_order_tv.setTextColor(getResources().getColor(R.color.theme_color));

                    trx.hide(fragments[prePosition]);
                    if (!fragments[index].isAdded()) {
                        trx.add(R.id.myorder_container_rl, fragments[index]);
                    }
                    trx.show(fragments[index]).commit();

                    prePosition = index;
                }
                break;
        }


    }
}
